class ApplicationController < ActionController::Base
  #protect_from_forgery
  require 'user_context.rb'
  require 'user.rb'  
  include ApplicationHelper
  
  def require_login
    user_context = session[:user_context]
    if user_context.nil?
      redirect_to :action => 'login', :controller => 'users', :status => 303
      return
    end
    if user_context.user.nil?
      redirect_to :action => 'login', :controller => 'users', :status => 303
      return
    end
    return true
  end
  
  def require_city    
    division = cookies["division"]    
    if division.nil? 
      redirect_to :action => 'choose_division', :controller => 'users', :status => 303
      return
    end    
    return true
  end
  
  def require_merchant_user
    user_context = session[:user_context]
    if require_login
      if user_context.merchant.nil?
        redirect_to :action => 'login', :controller => 'users', :status => 303
      else
        return true
      end
    end
  end
  
  def wrap_in_transaction
    ActiveRecord::Base.transaction do
      begin
        yield
      ensure
        raise ActiveRecord::Rollback
      end
    end
  end
  
end
